package bz.ziro.kanbe.controller.mail;

import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import bz.ziro.kanbe.bean.MailData;
import bz.ziro.kanbe.dao.MailDao;
import bz.ziro.kanbe.logic.MailLogic;
import bz.ziro.kanbe.model.Mail;

public class ReceiveController extends Controller {

    @Override
    public Navigation run() {
    	
        try {
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session, request.getInputStream());
            //メールデータの作成を行う
            MailData data = new MailData(message);

            List<Mail> mailList = MailDao.all();
            //無視する
            if ( mailList == null || mailList.size() <= 0 ) {
            	return null;
            }
 
            //Mailを探します。
            Mail mail = MailLogic.findMailData(data,mailList);
            //存在しなかった場合
            if ( mail == null ) {
            	return null;
            }
            //ページ作成を行う
            MailLogic.createPage(mail,data);

        } catch (MessagingException e) {
        } catch (Exception e) {
        }
   
        return null;
    }
}
